﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Admin
{
    public partial class DataCustomer : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void GV_CustomerConfirm_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmd_Delete")
            {
                string ID_Card = e.CommandArgs.CommandArgument.ToString();
                string Status = GV_CustomerConfirm.GetRowValuesByKeyValue(e.CommandArgs.CommandArgument, "Status").ToString();

                if (Status == "1")
                {
                    Delete_Customer(ID_Card);
                    GV_CustomerConfirm.DataBind();
                }
                else
                {
                    Delete_Customer(ID_Card);
                    MembershipUser user = Membership.GetUser(ID_Card);
                    Membership.DeleteUser(user.UserName, true);
                    GV_CustomerConfirm.DataBind();
                    //ลบจากระบบด้วย
                }
            }
        }
        public void Delete_Customer(string ID)
        {
            string sqltext = @"DELETE FROM [TB_Customer] WHERE ID_Card='"+ID+"'";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void Update_ComfirmUser(string Emp)
        {
            string sqltext = @"UPDATE TB_Customer SET Status=2  
                               WHERE [ID_Card] ='"+Emp+"' ";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        protected void bt_ConfirmCustomer_Click(object sender, EventArgs e)
        {
            encryption_MD5 MD5 = new encryption_MD5();
            for (int i = 0; i < GV_CustomerConfirm.VisibleRowCount; i++)
            {
                if (GV_CustomerConfirm.Selection.IsRowSelected(i)==true)
                {
                    object ID_Card = GV_CustomerConfirm.GetRowValues(i, "ID_Card");
                    object Status = GV_CustomerConfirm.GetRowValues(i, "Status");
                    object Pass = GV_CustomerConfirm.GetRowValues(i, "Pasword");
                    string PassWord = MD5.DecryptString(Pass.ToString());
                    if (Status.ToString() == "1")
                    {
                         MembershipUser user = Membership.CreateUser(ID_Card.ToString(),PassWord);
                         string[] rolesArray = Roles.GetAllRoles();
                         Roles.AddUserToRole(user.UserName, rolesArray[1]);
                         Update_ComfirmUser(ID_Card.ToString());
                    }
                }
                
            }
           GV_CustomerConfirm.Selection.UnselectAll();
            GV_CustomerConfirm.DataBind();
            
        }
    }
}